﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.基础设置;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
<<<<<<<< HEAD:MedicalPCTerminal.Write.API/Appcation/CommandHandlers/BasicSettingsCommandHandler/TeamInfoAddCommandHandler.cs
using MedicalPCTerminal.Write.API.Appcation.Commands.BasicSettingsCommand;

namespace MedicalPCTerminal.Write.API.Appcation.CommandHandlers.BasicSettingsCommandHandler
========
using MedicalPCTerminal.Write.API.Appcation.Commands.基础设置;

namespace MedicalPCTerminal.Write.API.Appcation.CommandHandlers.基础设置
>>>>>>>> parent of fe317f5 (修改命名空间):MedicalPCTerminal.Write.API/Appcation/CommandHandlers/基础设置/TeamInfoAddCommandHandler.cs
{
    /// <summary>
    /// 团队管理的添加处理命令
    /// </summary>
    public class TeamInfoAddCommandHandler : IRequestHandler<TeamInfoAddCommand, APIResult<int>>
    {
        /// <summary>
        /// sqlsugar
        /// </summary>
        private readonly SqlSugarContext sqlSugar;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="sqlSugar"></param>
        /// <param name="mapper"></param>
        public TeamInfoAddCommandHandler(SqlSugarContext sqlSugar,IMapper mapper)
        {
            this.sqlSugar = sqlSugar;
            this.mapper = mapper;
        }


        /// <summary>
        /// 处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<int>> Handle(TeamInfoAddCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            try
            {               
                var model = mapper.Map<TeamInfo>(request);
                model.IsDel = false;
                model.TeamStatus = false;
                var r = await sqlSugar.db.Insertable(model).ExecuteCommandAsync();
                if(r>0)
                {
                    result.Code = APIEnum.添加成功;
                    result.Msg = APIEnum.添加成功.ToString();
                    result.data = r;
                }
                else
                {
                    result.Code = APIEnum.添加失败;
                    result.Msg = APIEnum.添加失败.ToString();
                    result.data = r;
                }

               
            }
            catch (Exception ex)
            {

                // 记录异常信息
                Console.WriteLine($"添加团队信息时发生异常: {ex.Message}");
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
                result.data = 0;
            }
            return await Task.FromResult(result);
        }



    }
}
